package com.amaze.escape;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.logging.Level;

import org.lwjgl.opengl.GL20;
import org.newdawn.slick.util.ResourceLoader;

/**
 *
 * @author Amaze Team
 */
public class ShaderManager {

    private HashMap<String, Integer> _shaderPrograms = new HashMap<String, Integer>();
    private HashMap<String, Integer> _fragmentShader = new HashMap<String, Integer>();
    private HashMap<String, Integer> _vertexShader = new HashMap<String, Integer>();
    private static ShaderManager _instance = null;

    /**
     * Returns (and creates – if necessary) the static instance
     * of this helper class.
     *
     * @return The instance
     */
    public static ShaderManager getInstance() {
        if (_instance == null) {
            _instance = new ShaderManager();
        }


        return _instance;
    }

    /**
     * 
     */
    public ShaderManager() {
        initShader();
    }

    /**
     * 
     */
    private void initShader() {
        createVertexShader("fog_vert.glsl", "fog");
        createFragShader("fog_frag.glsl", "fog");

        for (String s : _fragmentShader.keySet()) {
            int shaderProgram = GL20.glCreateProgram();


            GL20.glAttachShader(shaderProgram, _fragmentShader.get(s));
            GL20.glAttachShader(shaderProgram, _vertexShader.get(s));
            GL20.glLinkProgram(shaderProgram);
            GL20.glValidateProgram(shaderProgram);

            _shaderPrograms.put(s, shaderProgram);
        }
    }

    private int createFragShader(String filename, String title) {

        _fragmentShader.put(title, GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER));

        if (_fragmentShader.get(title) == 0) {
            return 0;
        }

        String fragCode = "";
        String line;
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(ResourceLoader.getResource("com/amaze/escape/shader/" + filename).openStream()));
            while ((line = reader.readLine()) != null) {
                fragCode += line + "\n";
            }
        } catch (Exception e) {
            Helper.LOGGER.log(Level.SEVERE, "Failed reading fragment shading code.");
            return 0;
        }

        GL20.glShaderSource(_fragmentShader.get(title), fragCode);
        GL20.glCompileShader(_fragmentShader.get(title));

        printLogInfo(_fragmentShader.get(title));

        return _fragmentShader.get(title);
    }

    private int createVertexShader(String filename, String title) {

        _vertexShader.put(title, GL20.glCreateShader(GL20.GL_VERTEX_SHADER));

        if (_vertexShader.get(title) == 0) {
            return 0;
        }

        String fragCode = "";
        String line;
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(ResourceLoader.getResource("com/amaze/escape/shader/" + filename).openStream()));
            while ((line = reader.readLine()) != null) {
                fragCode += line + "\n";
            }
        } catch (Exception e) {
            Helper.LOGGER.log(Level.SEVERE, "Failed reading vertex shading code.");
            return 0;
        }

        GL20.glShaderSource(_vertexShader.get(title), fragCode);
        GL20.glCompileShader(_vertexShader.get(title));
        
        printLogInfo(_vertexShader.get(title));

        return _vertexShader.get(title);
    }

    private static void printLogInfo(int obj) {
        String output = GL20.glGetShaderInfoLog(obj, 1024);
        Helper.LOGGER.log(Level.INFO, "{0}", output);
    }

    /**
     * 
     * @param s
     */
    public void enableShader(String s) {
        if (s == null) {
            GL20.glUseProgram(0);
            return;
        }

        int shader = getShader(s);
        GL20.glUseProgram(shader);
    }

    /**
     * 
     * @param s
     * @return
     */
    public int getShader(String s) {
        return _shaderPrograms.get(s);
    }
}
